﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Text;
using System.Data.SqlClient;
using System.Data;
using BLL;

namespace DAL
{
    public class ServiceRole : IRoleService
    {
        private DbHelper DbHelper;
        public ServiceRole()
        {
            DbHelper = new DbHelper();
        }

        /// <summary>
        /// 删除
        /// </summary>
        public string Delete(int Id)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("delete from Role ");
            strSql.Append(" where RoleId=@RoleId ");
            SqlParameter[] parameters = {
					new SqlParameter("@RoleId", SqlDbType.Int,4)};
            parameters[0].Value = Id;

            int num = 0;
            num = DbHelper.ExecuteCommand(strSql.ToString(), parameters);
            if (num > 0)
                return "操作成功";
            else
                return "操作失败";
        }

        /// <summary>
        /// 更新
        /// </summary>
        public string Update(int id, int statusid)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("update Role set ");
            strSql.Append("StatusId=@StatusId");
            strSql.Append(" where RoleId=@RoleId ");
            SqlParameter[] parameters = {
					new SqlParameter("@StatusId", SqlDbType.Int,4),
                                        new SqlParameter("@RoleId", SqlDbType.Int,4)};
            parameters[0].Value = statusid;
            parameters[1].Value = id;

            int num = 0;
            num = DbHelper.ExecuteCommand(strSql.ToString(), parameters);
            if (num > 0)
                return "操作成功";
            else
                return "操作失败";
        }

        /// <summary>
        /// 根据ID返回
        /// </summary>
        public Role GetById(int Id)
        {
            ServiceStatusText ServiceStatusText = new ServiceStatusText();
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select  top 1 * from Role ");
            strSql.Append(" where RoleId=@RoleId ");
            SqlParameter[] parameters = {
					new SqlParameter("@RoleId", SqlDbType.Int,4)};
            parameters[0].Value = Id;

            using (DataTable table = DbHelper.GetDataTable(strSql.ToString(), parameters))
            {
                Role model = new Role();

                if (table.Rows.Count > 0)
                {
                    if (table.Rows[0]["RoleId"].ToString() != "")
                    {
                        model.RoleId = int.Parse(table.Rows[0]["RoleId"].ToString());
                    }
                    model.RoleName = table.Rows[0]["RoleName"].ToString();
                    if (table.Rows[0]["StatusId"].ToString() != "")
                    {
                        model.StatusId = int.Parse(table.Rows[0]["StatusId"].ToString());
                        model.StatusText = ServiceStatusText.GetById((int)model.StatusId);
                    }
                    return model;
                }
                else
                {
                    return null;
                }
            }
        }

        /// <summary>
        /// 返回根据条件
        /// </summary>
    

        public IList<Role> GetBySql(string roleName, string status)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select * ");
            strSql.Append(" FROM Role ");
            strSql.AppendFormat("where RoleName Like '%{0}%' AND StatusId = {1}", roleName, status);

            //SqlParameter[] parameters = new SqlParameter[]{
            //    new SqlParameter("@rolenamepar",SqlDbType.NVarChar,50),
            //    new SqlParameter("@statusnamepar",SqlDbType.NVarChar,50)};

            //parameters[0].Value = roleName;
            //parameters[1].Value = status;

            using (DataTable table = DbHelper.GetDataTable(strSql.ToString()))
            {
                ServiceStatusText ServiceStatusText = new ServiceStatusText();
                IList<Role> list = new List<Role>();
                for (int i = 0; i < table.Rows.Count; i++)
                {
                    Role model = new Role();
                    if (table.Columns.Contains("RoleId") && table.Rows[i]["RoleId"] != DBNull.Value)
                        model.RoleId = int.Parse(table.Rows[i]["RoleId"].ToString());
                    if (table.Columns.Contains("RoleName") && table.Rows[i]["RoleName"] != DBNull.Value)
                        model.RoleName = table.Rows[i]["RoleName"].ToString();
                    if (table.Columns.Contains("StatusId") && table.Rows[i]["StatusId"] != DBNull.Value)
                    {
                        model.StatusId = int.Parse(table.Rows[i]["StatusId"].ToString());
                        model.StatusText = ServiceStatusText.GetById((int)model.StatusId);
                    }

                    list.Add(model);
                }
                return list;
            }
        }







        public IList<Role> GetBySql(string roleName, int status)
        {
            throw new NotImplementedException();
        }
    }
}
